package org.vartist.atld.menu.activity.util.audio;

import java.io.File;
import java.util.ArrayList;

import org.vartist.atld.R;
import org.vartist.atld.menu.adapter.util.audio.AudioListAdapter;
import org.vartist.atld.menu.consts.LayoutConst;
import org.vartist.atld.menu.vo.util.audio.VoiceRecord;
import org.vartist.atld.util.audio.AudioUtil;
import org.vartist.atld.util.log.LogUtil;
import org.vartist.atld.util.ui.TitleUtil;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AudioActivity extends Activity implements OnItemClickListener {
	
	private ArrayList<VoiceRecord> mListData;
	private AudioListAdapter mAdapter;
	private ListView mList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogUtil.debugNativeHeap();
		
		TitleUtil.setContentView(this, LayoutConst.LAYOUT_FOR_AUDIO, getString(R.string.app_name), getString(R.string.menu_util_audio_name));
		
		getLayout();
		
	    mListData = new ArrayList<VoiceRecord>();
	    mListData = AudioUtil.getRecordList(this);
	    
	    if (mListData.size() > 0) {
		    mAdapter = new AudioListAdapter(this, R.layout.activity_util_audio_row, mListData);
		    mList.setAdapter(mAdapter);
		    mList.setSelector(R.drawable.common_listview_item_click);
		    mList.setOnItemClickListener(this);			
		} else {
			Toast.makeText(this, "mListData.size() is zero...", Toast.LENGTH_SHORT).show();
		}		
	}
	
	private void getLayout() {
		mList = (ListView)findViewById(R.id.voice_record_list_listview);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
		Uri uri = Uri.fromFile(new File(mListData.get(position).getmFilePath()));				
		intent.setDataAndType(uri, "audio/*");
		startActivity(intent);
	}
}